﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace LuckyDraw.WinUI
{
	public class TransParentListBox : ListBox
	{
		public TransParentListBox()
		{
			this.SetStyle(ControlStyles.UserPaint, true);
			this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
		}

		protected override void OnSelectedIndexChanged(EventArgs e)
		{
			this.Invalidate();
			base.OnSelectedIndexChanged(e);
			this.ItemHeight = 48;
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			if (this.Focused && this.SelectedItem != null)
			{
				Rectangle itemRect = this.GetItemRectangle(this.SelectedIndex);
				e.Graphics.FillRectangle(Brushes.LightBlue, itemRect);
			}

			for (int i = 0; i < Items.Count; i++)
			{
				StringFormat strFmt = new System.Drawing.StringFormat();
				strFmt.Alignment = StringAlignment.Near;
				strFmt.LineAlignment = StringAlignment.Center;

				Rectangle rect = this.GetItemRectangle(i);
				rect.Location = new Point(rect.Location.X, rect.Location.Y * 3);
				rect.Height = rect.Height * 3;
				e.Graphics.DrawString(this.GetItemText(this.Items[i]), this.Font, new SolidBrush(this.ForeColor), rect, strFmt);
			}

			base.OnPaint(e);
		}

	}

}
